package com.hilaver.dzmis.web.servlet.basicinformation;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiArmor;
import com.hilaver.dzmis.basicinfo.BiFactory;
import com.hilaver.dzmis.basicinfo.BiSwatch;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.service.impl.BiSwatchServiceImpl;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class BiSwatchServlet extends AbstractBaseServlet {
	
	public final static int ACTION_GET_MAX_REFERENCE_INDEX = 10;

	private static final long serialVersionUID = -4514538281621552309L;
		
	private BiSwatch biSwatch;

	private BiSwatchServiceImpl armorService;
	
	public BiSwatchServlet() {
		super();
		this.simpleName = BiSwatch.class.getSimpleName();
		this.fullName = BiSwatch.class.getName();
		this.armorService = new BiSwatchServiceImpl();
		this.armorService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			this.armorService.setLocale((Locale) request.getSession()
					.getAttribute(Constants.SESSION_LOCALE));
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		super.setAction(request);
		super.setAcUser(request, this.armorService);
		String rtn = "";
		switch (this.action) {
			case ACTION_GET: // "get one"
				super.setId(request);
				rtn = this.armorService.get(this.id);
				break;
			case ACTION_GET_WITH_DESCRIPTION:
				
				break;
			case ACTION_DELETE:
				super.setId(request);
				rtn = this.armorService.delete(this.id);
				break;
			case ACTION_GET_ALL_PAGINATION_FILTER:
				super.setPaginationParam(request);
				super.setFilterParam(request);
				rtn = this.armorService.getAllPagination(this.page, this.sort,
						this.dir, super.filters);
				break;
			case ACTION_EDIT:
				this.fillObj(request);
				rtn = this.armorService.edit((BiSwatch)this.obj);
				break;
			case ACTION_GET_MAX_REFERENCE_INDEX:
				String ctSwatchType = request.getParameter("ctSwatchType");
				rtn = this.armorService.getMaxReferenceIndex(ctSwatchType);
				break;
		}
		return rtn;
	}
	
	private void fillObj(HttpServletRequest request) throws Exception{
		super.fillObj(request);
		
		String fId = (String)getValue(request, "factoryId");
		if(!StringUtils.isEmpty(fId)){
			BiFactory biF = new BiFactory();
			biF.setId(Integer.parseInt(fId));
			((BiSwatch)this.obj).setBiFactory(biF);
		}
		
		String armorId = (String)getValue(request, "armorId");
		if(!StringUtils.isEmpty(armorId)){
			BiArmor biArmor = new BiArmor();
			biArmor.setId(Integer.parseInt(armorId));
			((BiSwatch)this.obj).setBiArmor(biArmor);
		}
	}
	
}
